<template>
  <div class="navs">
    <swiper>
      <swiper-slide
        style="display: flex; flex-wrap: wrap;"
        v-for="(page, index) in swiperPages"
        :key="index"
      >
        <div class="icon-item" v-for="item in page" :key="item.id">
          <img :src="item.url" alt="" />
          <span class="text">{{ item.title }}</span>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navsData: [
        {
          id: 0,
          url: '//s.qunarzz.com/homenode/images/touchheader/hotel.png',
          title: '酒店'
        },
        {
          id: 1,
          url: '//s.qunarzz.com/homenode/images/touchheader/flight.png',
          title: '飞机'
        },
        {
          id: 2,
          url: '//s.qunarzz.com/homenode/images/touchheader/train.png',
          title: '火车'
        },
        {
          id: 3,
          url: '//s.qunarzz.com/homenode/images/touchheader/piao.png',
          title: '度假'
        },
        {
          id: 4,
          url: '//s.qunarzz.com/homenode/images/touchheader/piao.png',
          title: '景点门票'
        },
        {
          id: 5,
          url: '//s.qunarzz.com/homenode/images/touchheader/hotel.png',
          title: '酒店'
        },
        {
          id: 6,
          url: '//s.qunarzz.com/homenode/images/touchheader/piao.png',
          title: '酒店'
        },
        {
          id: 7,
          url: '//s.qunarzz.com/homenode/images/touchheader/hotel.png',
          title: '酒店'
        },
        {
          id: 8,
          url: '//s.qunarzz.com/homenode/images/touchheader/hotel.png',
          title: '酒店'
        },
        {
          id: 9,
          url: '//s.qunarzz.com/homenode/images/touchheader/piao.png',
          title: '酒店'
        },
        {
          id: 10,
          url: '//s.qunarzz.com/homenode/images/touchheader/piao.png',
          title: '酒店'
        }
      ]
    }
  },
  computed: {
    swiperPages() {
      // 方法一
      // const pages = []
      // this.navsData.forEach((item, index) => {
      //   const page = Math.floor(index / 10)
      //   if (!pages[page]) {
      //     pages[page] = []
      //   }
      //   pages[page].push(item)
      // })
      // return pages

      // 方法二
      const max = 10
      const arr = []
      let smallArr = []
      this.navsData.forEach((c, index) => {
        if (smallArr.length === 0) {
          arr.push(smallArr)
        }
        smallArr.push(c)
        if (smallArr.length === max) {
          smallArr = []
        }
      })
      return arr
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .swiper-container {
  width: 100%;
  height: 0;
  padding-bottom: 50%;
}
.navs {
  overflow: hidden;
  height: 0;
  padding-bottom: 50%;
  box-sizing: border-box;
  .icon-item {
    width: 20%;
    height: 80px;
    text-align: center;
    margin-top: 10px;
    color: #000;
    img {
      width: 50px;
      height: 50px;
      display: block;
      margin: 0 auto;
      margin-bottom: 10px;
    }
    .text {
      color: #000;
    }
  }
}
</style>
